package cn.edu.gzccc.movie.service.impl;

import cn.edu.gzccc.movie.domain.Movie;
import cn.edu.gzccc.movie.repository.MovieRepository;
import cn.edu.gzccc.movie.service.MovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

@Service
public class MovieServiceImpl implements MovieService {

    @Autowired
    private MovieRepository movieRepository;

    /**
     * 新增一个电影
     *
     * @param movie
     */
    @Override
    public void add(Movie movie) {
        movie.setName(movie.getName());
        movie.setPhoto(movie.getPhoto());
        movie.setCreateDate(movie.getCreateDate());
        movie.setSynopsis(movie.getSynopsis());
        movieRepository.save(movie);
    }

    /**
     * 删除一个电影
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        movieRepository.delete(id);
    }

    /**
     * 更新一个电影
     *
     * @param movie
     */
    @Override
    public void update(Movie movie) {
        Movie movie1 = movieRepository.findOne(movie.getId());
        movie1.setName(movie.getName());
        movie1.setPhoto(movie.getPhoto());
        movie1.setCreateDate(movie.getCreateDate());
        movie1.setSynopsis(movie.getSynopsis());
        movieRepository.save(movie1);
    }

    /**
     * 查找一个电影
     *
     * @param id
     * @return
     */
    @Override
    public Movie findOne(Long id) {
        Movie movie = movieRepository.findOne(id);
        return movie;
    }

    /**
     * 获取当前电影id
     *
     * @param id
     * @return
     */
    @Override
    public String getMovieId(Long id) {
        Movie movie = findOne(id);
        String movieId = movie.getId().toString();
        return movieId;
    }

    /**
     * 分页
     *
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Movie> findMoiveNoCriteria(Integer page, Integer size) {
        Pageable pageable = new PageRequest(page, size, Sort.Direction.DESC, "id");
        return movieRepository.findAll(pageable);
    }

}
